/**
 * @author zhengjiachao
 * @since 2017-3-27
 */

ADF.model.DiagramModel = function(){
    ADF.model.DiagramModel.superclass.constructor.call(this);
    this.children = [];
}

ADF.model.DiagramModel.PROPERTY_CHILDREN = "children";

COM.util.defineClass(ADF.model.DiagramModel, ADF.model.BaseModel,{
    initModel: function() {
      ADF.model.DiagramModel.superclass.initModel.call(this);
      var children = new AGEF.PropertyDescriptor(ADF.model.DiagramModel.PROPERTY_CHILDREN, []);
      this.addPropertyDescriptor(children);
    },
    addChild: function(child){
        var children = this.getPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN), newChildren = children.slice(0, children.length);
        var layoutdata=new COM.draw2d.Rectangle();
        COM.util.ObjUtil.copyProperty(layoutdata,child.getLayoutData());
        child.getPropertyDescriptor(AGEF.BaseModel.PROPERTY_LAYOUTDATA).setEditorType("baseProperty.XYLayoutDataEditor");
        child.setLayoutData(layoutdata);
        newChildren.push(child);
        child.setParent(this);
        this.setPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN, newChildren);
    },
    removeChild: function(child){
        var children = this.getPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN), newChildren = children.slice(0, children.length);
        child.removeParent();
        COM.util.Array.removeItem(newChildren, child);
        this.setPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN, newChildren);
    },
    getChildren: function(){
        return this.getPropertyValue(ADF.model.DiagramModel.PROPERTY_CHILDREN);
    },

    _getModelType : function(){
        return "ADF.model.DiagramModel";
    },

    _getName : function(){
        return 'diagram';
    },

    fillSerializedJson : function(jsonObject){
        jsonObject["children"] = [];
        var children = this.getChildren();
        for(var index in children) {
            jsonObject.children.push(children[index].serialize());
        }
    },

    _deserialize: function(jsonObject) {
        var children = jsonObject["children"];
        for(var index in children) {
            var childJsonObject = COM.util.ObjUtil.parseJSON(children[index]), modelType = childJsonObject["modelType"], modelConstructor = eval("("+modelType+")");
            var childModel = new modelConstructor();
            childModel.deserialize(children[index]);
            this.addChild(childModel);
        }
    }

});
